package com.qf.agro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.Constant;
import com.qf.agro.common.CustomException;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.common.SysUserHolder;
import com.qf.agro.mapper.SysModuleMapper;
import com.qf.agro.pojo.SysModule;
import com.qf.agro.pojo.SysUser;
import com.qf.agro.service.SysModuleService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author 10202
 * @version V1.0
 * @Project smart-agriculture-parent
 * @Package com.qf.agro.service.impl
 * @Date 2023/5/22 17:52
 */
@Service
public class SysModuleServiceImpl implements SysModuleService {

    private final SysModuleMapper sysModuleMapper;
    private final RedisTemplate<Object,Object> redisTemplate;

    public SysModuleServiceImpl(SysModuleMapper sysModuleMapper, RedisTemplate<Object, Object> redisTemplate) {
        this.sysModuleMapper = sysModuleMapper;
        this.redisTemplate = redisTemplate;
    }


    @Override
    public ResultInfo getMenuList(Long id) {
        List<SysModule> menuList=null;
        menuList = (List<SysModule>) redisTemplate.opsForValue().get(Constant.PERMISSION_LIST_USER_ID_PREFIX + id);

        if(menuList!=null&&menuList.size()>0){
            return ResultInfo.success(menuList);
        }

        menuList = sysModuleMapper.getListMenuList(id);
        System.out.println(menuList);

        if(menuList!=null){
            for (SysModule sysModule : menuList) {
                Long pId = sysModule.getId();
                LambdaQueryWrapper<SysModule> lqw=new LambdaQueryWrapper<>();
                lqw.eq(SysModule::getParentId,pId);
                List<SysModule> sysModules = sysModuleMapper.selectList(lqw);
                sysModule.setChildren(sysModules);
            }
        }
        redisTemplate.opsForValue().set(Constant  .PERMISSION_LIST_USER_ID_PREFIX + id,menuList);

        return ResultInfo.success(menuList);
    }

    @Override
    public ResultInfo getMenu() {


        LambdaQueryWrapper<SysModule> lqw =new LambdaQueryWrapper<>();
        lqw.isNull(SysModule::getParentId);
        List<SysModule> sysModules = sysModuleMapper.selectList(lqw);

        for (SysModule sysModule : sysModules) {

            Long id = sysModule.getId();
            LambdaQueryWrapper<SysModule> lqw1 =new LambdaQueryWrapper<>();
            lqw1.eq(SysModule::getParentId,id);
            List<SysModule> modules = sysModuleMapper.selectList(lqw1);
            sysModule.setChildren(modules);
        }


        return ResultInfo.success(sysModules);
    }

    @Override
    public ResultInfo getRoleMessage(Long roleId) {

        List<Long> moduleIdList=sysModuleMapper.getRoleMessage(roleId);


        return ResultInfo.success(moduleIdList);
    }

    @Transactional(rollbackFor = CustomException.class)
    @Override
    public ResultInfo updateRoleMessage(Map<String, Object> roleMessageMap) {
        String roleId = (String) roleMessageMap.get("roleId");
        List<String> moduleIds = (List<String>) roleMessageMap.get("moduleIds");
        System.out.println(moduleIds);
        //删除
        ResultInfo resultInfo1 = this.deleteRoleMessage(roleId);

        ResultInfo resultInfo2 = this.insertRoleMessage(roleId, moduleIds);

        Integer code1 = resultInfo1.getCode();
        Integer code2 = resultInfo2.getCode();


        if(code1==1&&code2==1){
            redisTemplate.delete(Constant.PERMISSION_LIST_USER_ID_PREFIX+SysUserHolder.get().getId());
        }

        return code1==1&&code2==1?ResultInfo.success("更新成功"):ResultInfo.error("更新失败");
    }

    @Override
    public ResultInfo deleteRoleMessage(String roleId) {
        return sysModuleMapper.deleteModleByRoleId(roleId)?ResultInfo.success("删除成功"):ResultInfo.error("删除失败");
    }

    @Override
    public ResultInfo insertRoleMessage(String roleId, List<String> moduleIds) {
        boolean success=true;
        for (String moduleId : moduleIds) {
            if(!sysModuleMapper.updateByRoleId(roleId,moduleId)){
                success=false;
                break;
            }
        }

        return success?ResultInfo.success("新增成功"):ResultInfo.error("新增失败");
    }

    @Override
    public ResultInfo getPageModule(Long pageNum, Long pageSize) {

        Page<SysModule> page=new Page<>(pageNum,pageSize);
     page= sysModuleMapper.selectPage(page,null);
        return ResultInfo.success(page);
    }
}
